/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Object.BookProperties;

import Object.Database;
import Object.Page;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author PhongLK60473
 */
public class Author {
    private int authorId;
    private String authorName;
    private String authorDescription;

    public Author() {
    }

    public Author(String authorName) {
        this.authorName = authorName;
    }

    public Author(int authorId) {
        this.authorId = authorId;
    }
    
    public Author(int authorId, String authorName) {
        this.authorId = authorId;
        this.authorName = authorName;
        this.authorDescription = "";
    }

    public String getAuthorDescription() {
        return authorDescription;
    }

    public Author(int authorId, String authorName, String authorDescription) {
        this.authorId = authorId;
        this.authorName = authorName;
        this.authorDescription = (authorDescription==null)?"":authorDescription;
    }



    public void setAuthorDescription(String authorDescription) {
        this.authorDescription = authorDescription;
    }



    public static Author[] AllAuthors(){
        ArrayList alAuthor = new ArrayList();
        Database db = new Database();

        if(db.isOK){
            String strSQL;
            PreparedStatement stm=null;
            ResultSet rs = null;
            try {
                strSQL = "SELECT * FROM Authors";
                stm = db.con.prepareStatement(strSQL);
                rs = stm.executeQuery();

                while(rs.next()){
                    Author author = new Author(rs.getInt(1),rs.getString(2),rs.getString(3));
                    alAuthor.add(author);
                }
            } catch (SQLException e) {
                System.out.println("Author(){ SQL : "+e.getMessage());
            } finally {
                try{
                    rs.close();
                    stm.close();
                    db.con.close();
                } catch (SQLException e){
                }
            }
        }else{
            System.out.println("Author(){ Connect database failed }");
        }

        Author[] authors = new Author[alAuthor.size()];
        alAuthor.toArray(authors);
        return authors;
    }
    public int getAuthorId() {
        return authorId;
    }

    public void setAuthorId(int authorId) {
        this.authorId = authorId;
    }

    public String getAuthorName() {
        return authorName;
    }

    public void setAuthorName(String authorName) {
        this.authorName = authorName;
    }


    public static boolean AddAuthor(HttpServletRequest request){
        boolean rt=false;
        try{
            String name = request.getParameter("txtName").toString()+"";

            Author author = new Author(name);
            rt=author.Add();
        }catch(Exception e){
            rt=false;
            e.printStackTrace();
        }
        if(!rt){
            Page.setMessage("Add Author failed!", request);
        }else{
            Page.setMessage("Add Author successfull!", request);
        }
        return rt;
    }
    public boolean Add(){
        boolean rt=false;
        Database db = new Database();
        if(db.isOK){
            String strSQL;
            PreparedStatement stm=null;
            int rs;
            try {
                strSQL = "INSERT INTO Authors (name)"
                        + " VALUES(?)";
                stm = db.con.prepareStatement(strSQL);
                stm.setString(1, this.authorName);

                rs = stm.executeUpdate();

                if(rs>0){
                    rt=true;
                }else{
                    rt=false;
                }
            } catch (SQLException e) {
                rt=false;
            } finally {
                try{
                    stm.close();
                    db.con.close();
                } catch (SQLException e){

                }
            }
        }else{
            rt=false;
        }
        return rt;
    }
    public static boolean SaveAuthor(HttpServletRequest request){
        boolean rt=false;
        try{
            String name = request.getParameter("txtName").toString()+"";
            int authorid = Integer.parseInt(request.getParameter("authorid"));
            Author author = new Author(authorid, name);
            rt=author.Save();
        }catch(Exception e){
            rt=false;
            e.printStackTrace();
        }
        if(!rt){
            Page.setMessage("Edit Author failed!", request);
        }else{
            Page.setMessage("Edit Author successfull!", request);
        }
        return rt;
    }
    public boolean Save(){
        boolean rt=false;
        Database db = new Database();
        if(db.isOK){
            String strSQL;
            PreparedStatement stm=null;
            int rs;
            try {
                strSQL = "UPDATE Authors SET name = ? "
                        + "WHERE id = ?";
                stm = db.con.prepareStatement(strSQL);
                stm.setString(1, this.authorName);
                stm.setInt(2, this.authorId);
                
                rs = stm.executeUpdate();

                if(rs>0){
                    rt=true;
                }else{
                    rt=false;
                }
            } catch (SQLException e) {
                rt=false;
            } finally {
                try{
                    stm.close();
                    db.con.close();
                } catch (SQLException e){

                }
            }
        }else{
            rt=false;
        }
        return rt;
    }
    public static boolean DeleteAuthor(HttpServletRequest request){
        boolean rt=false;
        try{
            int authorid = Integer.parseInt(request.getParameter("authorid"));
            Author author = new Author(authorid);
            rt=author.Delete();
        }catch(Exception e){
            rt=false;
            e.printStackTrace();
        }
        if(!rt){
            Page.setMessage("Delete Author failed!", request);
        }else{
            Page.setMessage("Delete Author successfull!", request);
        }
        return rt;
    }
    public boolean Delete(){
        boolean rt=false;
        Database db = new Database();
        if(db.isOK){
            String strSQL;
            PreparedStatement stm=null;
            int rs;
            try {
                strSQL = "DELETE Authors WHERE id = ?";
                stm = db.con.prepareStatement(strSQL);
                stm.setInt(1, this.authorId);

                rs = stm.executeUpdate();

                if(rs>0){
                    rt=true;
                }else{
                    rt=false;
                }
            } catch (SQLException e) {
                rt=false;
            } finally {
                try{
                    stm.close();
                    db.con.close();
                } catch (SQLException e){

                }
            }
        }else{
            rt=false;
        }
        return rt;
    }
}
